| Conditions | 12 |
| Paths | 256 |
| Total Lines | 159 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 15 | ||
| Bugs | 1 | Features | 3 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like map.js ➔ initialize often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | /*jslint |
||
| 379 | function initialize(xcenter, xzoom, xmap, xfeatures, xmarkers, xlines, xgeocache) { |
||
| 380 | 'use strict'; |
||
| 381 | |||
| 382 | var center, |
||
| 383 | atDefaultCenter = false, |
||
| 384 | zoom = parseInt(xzoom, 10), |
||
| 385 | maptype = xmap, |
||
| 386 | loadfromcookies = false, |
||
| 387 | markerdata = parseMarkersFromUrl(xmarkers), |
||
| 388 | markercenter = null, |
||
| 389 | clat = 0, |
||
| 390 | clon = 0; |
||
| 391 | if (markerdata.length > 0) { |
||
| 392 | markerdata.map(function (m) { |
||
| 393 | clat += m.coords.lat(); |
||
| 394 | clon += m.coords.lng(); |
||
| 395 | }); |
||
| 396 | markercenter = new google.maps.LatLng(clat / markerdata.length, clon / markerdata.length); |
||
| 397 | } |
||
| 398 | |||
| 399 | if (xcenter && xcenter !== '') { |
||
| 400 | center = parseCenterFromUrl(xcenter); |
||
| 401 | } else if (markercenter) { |
||
| 402 | center = markercenter; |
||
| 403 | } else { |
||
| 404 | loadfromcookies = true; |
||
| 405 | |||
| 406 | /* try to read coordinats from cookie */ |
||
| 407 | clat = get_cookie_float('clat', CLAT_DEFAULT); |
||
| 408 | clon = get_cookie_float('clon', CLON_DEFAULT); |
||
| 409 | if (clat === CLAT_DEFAULT && clon === CLON_DEFAULT) { |
||
| 410 | atDefaultCenter = true; |
||
| 411 | } |
||
| 412 | |||
| 413 | clat = repairLat(clat, CLAT_DEFAULT); |
||
| 414 | clon = repairLon(clon, CLON_DEFAULT); |
||
| 415 | center = new google.maps.LatLng(clat, clon); |
||
| 416 | |||
| 417 | zoom = get_cookie_int('zoom', ZOOM_DEFAULT); |
||
| 418 | maptype = get_cookie_string('maptype', MAPTYPE_DEFAULT); |
||
| 419 | } |
||
| 420 | |||
| 421 | if (!center) { |
||
| 422 | center = new google.maps.LatLng(CLAT_DEFAULT, CLON_DEFAULT); |
||
| 423 | atDefaultCenter = true; |
||
| 424 | } |
||
| 425 | |||
| 426 | zoom = repairZoom(zoom, ZOOM_DEFAULT); |
||
| 427 | maptype = repairMaptype(maptype, MAPTYPE_DEFAULT); |
||
| 428 | map = new google.maps.Map( |
||
| 429 | document.getElementById("themap"), |
||
| 430 | { |
||
| 431 | zoom: zoom, |
||
| 432 | center: center, |
||
| 433 | scaleControl: true, |
||
| 434 | streetViewControl: false, |
||
| 435 | mapTypeControlOptions: { mapTypeIds: ['OSM', 'OSM/DE', 'OCM', 'OUTD', 'TOPO', google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.SATELLITE, google.maps.MapTypeId.HYBRID, google.maps.MapTypeId.TERRAIN] }, |
||
| 436 | mapTypeId: google.maps.MapTypeId.ROADMAP |
||
| 437 | } |
||
| 438 | ); |
||
| 439 | |||
| 440 | map.mapTypes.set("OSM", osmProvider("OSM")); |
||
| 441 | map.mapTypes.set("OSM/DE", osmDeProvider("OSM/DE")); |
||
| 442 | map.mapTypes.set("OCM", thunderforestProvider("OCM", "cycle", API_KEY_THUNDERFOREST)); |
||
| 443 | map.mapTypes.set("OUTD", thunderforestProvider("OUTD", "outdoors", API_KEY_THUNDERFOREST)); |
||
| 444 | map.mapTypes.set("TOPO", opentopomapProvider("TOPO")); |
||
| 445 | map.setMapTypeId(maptype); |
||
| 446 | |||
| 447 | Attribution.init(map); |
||
| 448 | Sidebar.init(map); |
||
| 449 | ExternalLinks.init(map); |
||
| 450 | Markers.init(map); |
||
| 451 | Lines.init(map); |
||
| 452 | Geolocation.init(map); |
||
| 453 | Hillshading.init(map); |
||
| 454 | NPA.init(map); |
||
| 455 | CDDA.init(map); |
||
| 456 | Freifunk.init(map); |
||
| 457 | Okapi.init(map); |
||
| 458 | DownloadGPX.init(map); |
||
| 459 | |||
| 460 | //boundariesLayer = new google.maps.ImageMapType({ |
||
| 461 | // getTileUrl: function(coord, zoom) { |
||
| 462 | // if (6 <= zoom && zoom <= 16) |
||
| 463 | // { |
||
| 464 | // return tileUrl("http://korona.geog.uni-heidelberg.de/tiles/adminb/?x=%x&y=%y&z=%z", ["dummy"], coord, zoom); |
||
| 465 | // } |
||
| 466 | // else |
||
| 467 | // { |
||
| 468 | // return null; |
||
| 469 | // } |
||
| 470 | // }, |
||
| 471 | // tileSize: new google.maps.Size(256, 256), |
||
| 472 | // name: "adminb", |
||
| 473 | // alt: "Administrative Boundaries", |
||
| 474 | // maxZoom: 16 }); |
||
| 475 | |||
| 476 | map.setCenter(center, zoom); |
||
| 477 | |||
| 478 | google.maps.event.addListener(map, "center_changed", function () { |
||
| 479 | storeZoom(); |
||
| 480 | storeCenter(); |
||
| 481 | }); |
||
| 482 | google.maps.event.addListener(map, "zoom_changed", function () { |
||
| 483 | storeZoom(); |
||
| 484 | storeCenter(); |
||
| 485 | }); |
||
| 486 | google.maps.event.addListener(map, "maptypeid_changed", function () { |
||
| 487 | storeMapType(); |
||
| 488 | }); |
||
| 489 | |||
| 490 | if (loadfromcookies) { |
||
| 491 | parseMarkersFromCookies().map(function (m) { |
||
| 492 | Markers.newMarker(m.coords, m.id, m.r, m.name); |
||
| 493 | }); |
||
| 494 | |||
| 495 | parseLinesFromCookies().map(function (m) { |
||
| 496 | Lines.newLine(m.source, m.target); |
||
| 497 | }); |
||
| 498 | } else { |
||
| 499 | markerdata.map(function (m) { |
||
| 500 | Markers.newMarker(m.coords, m.id, m.r, m.name); |
||
| 501 | }); |
||
| 502 | |||
| 503 | parseLinesFromUrl(xlines).map(function (m) { |
||
| 504 | Lines.newLine(m.source, m.target); |
||
| 505 | }); |
||
| 506 | } |
||
| 507 | |||
| 508 | Okapi.setShowCache(xgeocache); |
||
| 509 | Sidebar.restore(true); |
||
| 510 | xfeatures = xfeatures.toLowerCase(); |
||
| 511 | if (xfeatures === '[default]') { |
||
| 512 | Hillshading.restore(false); |
||
| 513 | //restoreBoundaries(false); |
||
| 514 | Okapi.restore(false); |
||
| 515 | NPA.toggle(false); |
||
| 516 | CDDA.toggle(false); |
||
| 517 | Freifunk.toggle(false); |
||
| 518 | } else { |
||
| 519 | Hillshading.toggle(xfeatures.indexOf('h') >= 0); |
||
| 520 | //toggleBoundaries(xfeatures.indexOf('b') >= 0); |
||
| 521 | Okapi.toggle(xfeatures.indexOf('g') >= 0); |
||
| 522 | NPA.toggle(xfeatures.indexOf('n') >= 0); |
||
| 523 | Freifunk.toggle(xfeatures.indexOf('f') >= 0); |
||
| 524 | } |
||
| 525 | restoreCoordinatesFormat("DM"); |
||
| 526 | |||
| 527 | if (xgeocache !== "") { |
||
| 528 | Okapi.toggle(true); |
||
| 529 | atDefaultCenter = false; |
||
| 530 | } |
||
| 531 | |||
| 532 | Attribution.forceUpdate(); |
||
| 533 | |||
| 534 | if (atDefaultCenter) { |
||
| 535 | Geolocation.whereAmI(); |
||
| 536 | } |
||
| 537 | } |
||
| 538 |